﻿using Newtonsoft.Json;
using QingZhouLinYe_Web.DbContext;
using QingZhouLinYe_Web.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Security;
using System.Web.Services.Description;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace QingZhouLinYe_Web
{
    public partial class Login : System.Web.UI.Page
    {
        private DbServices dbServices = new DbServices();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void loginbutton_Click(object sender, EventArgs e)
        {
            /**
             1、用户登录
                 默认用户名：admin，初始密码：admin
             2、检测登录用户是否是admin，用户密码是否是admin
                是，弹出更改用户名和密码页面
                否，进入主页面（index.html）
            **/
            UserInfo user = dbServices.userLogin(username.Text.Trim(), password.Text.Trim());  //用户登录后获取用户
            if (user != null)   //存在用户
            {
                //检查admin用户是否更改密码
                if (user.Name == "admin" && user.PassWord == "admin")  //如果是初始密码
                {
                    //跳转到更改密码页面
                    FormsAuthentication.SetAuthCookie(user.Name, false);
                    Server.Transfer("Login/EditPassWord.html?userId="+user.Id);
                }
                else
                {
                    //跳转到主页(Index.aspx);
                    // Server.Transfer("index.html");
                    //FormsAuthentication.SetAuthCookie(user.Name, false);
                    //创建登录用户对象
                    LoginUser loginuser = new LoginUser();
                    loginuser.Id = user.Id;
                    loginuser.Name = user.Name;
                    loginuser.FangHuoDanWei = user.FangHuoDanWei;
                    loginuser.FangHuoDanWei.CenterShape = user.FangHuoDanWei.CenterShape;
                    loginuser.Authority = user.Authority;
                    string loginuser_str = JsonConvert.SerializeObject(loginuser);  //登录对象序列化成字符串
                    string user_str = JsonConvert.SerializeObject(user);  //登录对象序列化成字符串

                    Debug.WriteLine(loginuser_str); 
                    Debug.WriteLine(user_str);

                    //设置Ticket信息
                    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
                        1, loginuser.Name, DateTime.Now, DateTime.Now.AddMinutes(1000), false, loginuser_str);

                    //加密验证票据
                    string strTicket = FormsAuthentication.Encrypt(ticket);

                    //使用登录用户信息保存cookie
                    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName,strTicket);
                    cookie.Expires = ticket.Expiration;
                    this.Response.Cookies.Add(cookie);

                    //跳转到主页面
                    Response.Redirect("index.aspx");
                }
            }
            else   //不存在用户
            {
              MsgLabel.Text = "用户不存在，请联系管理员注册";
            }


        }
    }
}